package com.aaa.controller;

import com.aaa.entity.Album;
import com.aaa.entity.Voice;
import com.aaa.service.AlbumService;
import com.aaa.service.VipService;
import com.aaa.service.VoiceService;
import com.aaa.util.FileUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("voice")
public class VoiceController {
    @Resource
    VoiceService voiceService;
    @Resource
    AlbumService albumService;
    @Resource
    VipService vipService;

    @RequestMapping("selectVoice")
    @ResponseBody
    public PageInfo select(Integer pageNum, Integer pageSize, String vname) throws ParseException {
        if(vname!=null){
            //模糊查询专辑名根据
            PageHelper.startPage(pageNum,pageSize);
            List<Voice> list=voiceService.selectVoice(vname);
            PageInfo pageInfo=new PageInfo(list);
            return  pageInfo;
        }else {
            //查询全部
            PageHelper.startPage(pageNum,pageSize);
            List<Voice> list=voiceService.selectVoiceAll();
            int size=list.size();
            SimpleDateFormat sdf = new SimpleDateFormat("YYY-MM-DD HH:mm:ss");

            PageInfo pageInfo=new PageInfo(list);

            return  pageInfo;
        }
    }

    //批量删除
    @RequestMapping("delVoice")
    @ResponseBody
    public boolean delVoice(String str) {
    String[] vid= str.split(",");
        boolean b=false;
    for (int i=0;i<vid.length;i++) {
        b = voiceService.removeById(vid[i]);
    }
    return b;
    }
    //音频修改
    @RequestMapping("updVoice")
    @ResponseBody
    public boolean updVoice(Voice voice) {
        boolean b = voiceService.updateById(voice);
        return true;
    }
    //上传音频返回主键
    @RequestMapping("upload")
    @ResponseBody
    public long upload(@RequestParam("file") MultipartFile file, Integer aid, HttpServletRequest request) {
    String  vPath= FileUtil.upload(file,"",request);
        Voice voice=new Voice();
        voice.setAid(aid);
        voice.setVpath(vPath);
        boolean save = voiceService.save(voice);
        if(save!=false){
            return voice.getVid();
        }else {
            return 0;
        }
    }
    //音频添加(根据添加音频返回的主键进行修改)
    @RequestMapping("voiceAdd")
    @ResponseBody
    public boolean voiceAdd(Voice voice) {
        voice.setNum(0);
        voice.setUploadtime(new Date());
        boolean result = voiceService.updateById(voice);
        return result;
    }

}
